import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from "@/views/Login.vue"
import { Toast } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    name: "/home",
    component: Login,
    meta: {
      title: "登录"
    }
  },
  {
    path: "/register",
    name: "register",
    component: () => import("@/views/Register.vue"),
    meta: {
      title: "注册"
    }
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    meta: {
      title: "首页",
      showTab: true//显示底部菜单
    },
  },
  {
    path: "/board",
    name: "board",
    component: () => import("@/views/Board.vue"),
    meta: {
      title: "宠物寄养",
      showTab: true//显示底部菜单
    }
  },
  {
    path: "/order",
    name: "order",
    component: () => import("@/views/Order.vue"),
    meta: {
      title: "订单",
      showTab: true//显示底部菜单
    }
  },
  {
    path: "/mine",
    name: "mine",
    component: () => import("@/views/Mine.vue"),
    meta: {
      title: "我的",
      showTab: true//显示底部菜单
    }
  },
  {
    path: "/diaryList",
    name: "diaryList",
    component: () => import("@/views/DiaryList.vue"),
    meta: {
      title: "伴宠日志"
    }
  },
  {
    path: "/diarydetail",
    name: "diarydetail",
    component: () => import("@/views/DiaryDetail.vue"),
    meta: {
      title: "伴宠日志详情"
    }
  },
  {
    path: "/boarddetail",
    name: 'boarddetail',
    component: () => import("@/views/BoardDetail.vue"),
    meta: {
      title: "商家详情"
    }
  },
  {
    path: "/reservation/:b_id/:b_name/:b_price",
    name: "reservation",
    component: () => import("@/views/Reservation"),
    meta: {
      title: "立即预定"
    },
    props: true
  },
  {
    path: "/contactAdd",
    name: "contactAdd",
    component: () => import("@/views/ContactAdd"),
    meta: {
      title: "新增联系人"
    },
  },
  {
    path: "/contactEdit",
    name: "contactEdit",
    component: () => import("@/views/ContactEdit"),
    meta: {
      title: "联系人列表"
    },
    // props: true
  },
  {
    path: "/contactUpdate/:id",
    name: "contactUpdate",
    component: () => import("@/views/ContactUpdate"),
    meta: {
      title: "联系人修改"
    },
    props: true
  },
  {
    path: "/pet",
    name: "pet",
    component: () => import("@/views/Pet.vue"),
    meta: {
      title: "宠物资料"
    },

  }
]

const scrollBehavior = function scrollBehavior(to, from, savedPosition) {
  if (savedPosition) {
    return savedPosition;
  } else {
    return { x: 0, y: 0 }
  }
};

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior,
})




// 全局路由守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/login" || to.path == "/register") {
    next()
  } else {
    let token = sessionStorage.getItem("token")
    if (token) {
      next()
    } else {
      Toast.fail("请先登录!")
      next("/login")
    }
  }
})




export default router
